package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Change_pwdActivity extends AppCompatActivity {

    private TextView change_password_userNames;
    private EditText change_password_userName;
    private EditText change_password_old;
    private EditText change_password_new;
    private EditText change_password_new2;
    private Button change_password_baocun;
    private ImageView change_password_baocun_back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        initid();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        if (id.equals("Login")){
            change_password_userName.setVisibility(View.VISIBLE);
            change_password_userNames.setVisibility(View.GONE);
        }else {
            change_password_userName.setVisibility(View.GONE);
            change_password_userNames.setVisibility(View.VISIBLE);
            change_password_userNames.setText(id);
        }

        change_password_baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (change_password_new.getText().toString().equals(change_password_new2.getText().toString())){
                    if (id.equals("Login")){
                        xiugaimima(change_password_old.getText().toString(),change_password_new.getText().toString(),change_password_userName.getText().toString());
                    }
                    else {
                        xiugaimima(change_password_old.getText().toString(),change_password_new.getText().toString(),change_password_userNames.getText().toString());
                    }
                }else {
                    Toast.makeText(Change_pwdActivity.this,"两次输入的新密码不同",Toast.LENGTH_SHORT).show();
                }
            }
        });
        change_password_baocun_back = (ImageView)findViewById(R.id.change_password_baocun_back);
        change_password_baocun_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initid(){
        change_password_userNames = (TextView) findViewById(R.id.change_password_userNames);
        change_password_userName = (EditText) findViewById(R.id.change_password_userName);
        change_password_old = (EditText)findViewById(R.id.change_password_old);
        change_password_new = (EditText)findViewById(R.id.change_password_new);
        change_password_new2 = (EditText)findViewById(R.id.change_password_new2);
        change_password_baocun = (Button)findViewById(R.id.change_password_baocun);
    }

    private void xiugaimima(String oldpwd,String newpwd,String uesrname){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("newPassword",change_password_new.getText().toString());
                    jsonObject.put("oldPassword",change_password_old.getText().toString());
                    MediaType type = MediaType.parse("application/json;charset=utf-8");
                    RequestBody requestBody = RequestBody.create(type,""+jsonObject.toString());
                    OkHttpClient client = new OkHttpClient();
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/User/UpdatePassword?oldPwd="+oldpwd+"&newPwd="+newpwd+"&userName="+uesrname)
                            .put(requestBody)
                            .build();
                    Response response = client.newCall(request1).execute();
                    String data = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(data);
                    String message = jsonObject1.getString("message");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (message.equals("succeed")){
                                Toast.makeText(Change_pwdActivity.this,"修改密码成功!",Toast.LENGTH_SHORT).show();
                                Sto.State=0;
                                finish();
                            }else {
                                Toast.makeText(Change_pwdActivity.this,message,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}